<?php

namespace App\Console\Commands;

use App\Menu;
use App\Services\MenuService;
use Illuminate\Console\Command;

class CreateMenuController extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'menu:create';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create controller classes belong to menus data in db';

    /**
     * Create a new command instance.
     *
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $menus = Menu::all();
        foreach ($menus as $menu) {
            $group_name = $menu->group;
            assert(in_array($group_name, MenuService::GROUPS), 'unknown group name');
            $controller = sprintf("%sController", $group_name);
            $action = $menu->key;
            $class_name = "\\App\\Http\\Controller\\$controller";
            if (!class_exists($class_name)){
                exec('php artisan make:controller '.$controller, $output);
                array_map(function($_msg) {
                    $this->info($_msg);
                }, $output);
            }
            if (!method_exists($class_name, $action)) {
                
            }
        }
    }
}
